package com.rocksea.app.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.rocksea.app.interfaces.MyExecuteCallBack;
import com.rocksea.app.R;
import com.rocksea.app.adapter.PileAdapter;
import com.rocksea.app.db.DbCenter;
import com.rocksea.app.entry.CommitBean;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.utils.BaseAskTask;
import com.rocksea.app.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吴志华 on 2017/10/20.
 *
 * @FileName:com.rocksea.app.dialog.CommitInspectionDialog.java
 */

public class CommitInspectionDialog extends Dialog
{
    public static final int MODE_PASS = 0;
    public static final int MODE_OVERPASS =1;
    public static final int MODE_ALLUNPASS =2;

    private int mode;
    private InspectionForm.InspectionFormListBean inspectionFormList;
    private EditText mReamkEdit;
    private PileAdapter pileAdapter;
    private View contentView;
    private ProgressBar progressBar;
    private ListView mPiles;
    private EditText mErrorText;

    public CommitInspectionDialog(Context context, InspectionForm.InspectionFormListBean inspectionFormList)
    {
        this(context);
        this.inspectionFormList = inspectionFormList;
        setTitle("操作报检单");
    }

    private CommitInspectionDialog(@NonNull Context context) {
        super(context);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (window!=null)
        {
            WindowManager m = getWindow().getWindowManager();
            Display d = m.getDefaultDisplay();
            WindowManager.LayoutParams p = getWindow().getAttributes();
            p.width = d.getWidth()-60;
            getWindow().setAttributes(p);
        }
        contentView = findViewById(R.id.Main_Content);
        progressBar = (ProgressBar) findViewById(R.id.ProgressBar);

        final TextView textView1 = (TextView) findViewById(R.id.title1);
        mPiles = (ListView) findViewById(R.id.piles);
        ArrayList<String> pileNames = new ArrayList<>();
        mReamkEdit = (EditText) findViewById(R.id.reason);
        mErrorText = (EditText) findViewById(R.id.error);
        final CheckBox mCheckAll = (CheckBox) findViewById(R.id.Choose_All);
        mCheckAll.setVisibility(View.GONE);
        if (inspectionFormList.pileList!=null)
        {
            for (int i = 0; i < inspectionFormList.pileList.size(); i++)
            {
                pileNames.add(inspectionFormList.pileList.get(i).pileNo);
            }
            exeQuery(inspectionFormList.number,pileNames);
        }
        else
        {
            progressBar.setVisibility(View.GONE);
            contentView.setVisibility(View.VISIBLE);
            ArrayList<CommitBean> commitBeens = new ArrayList<>();
            pileAdapter = new PileAdapter(commitBeens,getContext(),mode,onPileErrorMessage);
            mPiles.setAdapter(pileAdapter);
            UiUtils.showToast("该报检单下没有桩");
        }
        RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.action);
        mCheckAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    pileAdapter.chooseAll();
                }
                else
                {
                    pileAdapter.unChooseAll();
                }
            }
        });
        mode = MODE_PASS;
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId)
            {
                switch (checkedId)
                {
                    case R.id.pass:
                        textView1.setText("提交已上传的桩");
                        mode = MODE_PASS;
                        mCheckAll.setVisibility(View.GONE);
                        break;
                    case R.id.unPass:
                        textView1.setText("选择要驳回的桩");
                        mode = MODE_OVERPASS;
                        mCheckAll.setVisibility(View.VISIBLE);
                        break;
                    case R.id.All_UnPass:
                        textView1.setText("全部驳回");
                        mode = MODE_ALLUNPASS;
                        mCheckAll.setVisibility(View.GONE);
                        pileAdapter.chooseAll();
                        break;
                }
                if (pileAdapter!=null)
                    pileAdapter.changeState(mode);
            }
        });

        findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Integer> choosePosition = pileAdapter.getCheckPosition();
                String remake = mReamkEdit.getText().toString().trim();
                int counts = pileAdapter.getUpLoadCounts();
                switch (mode){
                    case MODE_PASS:
                        if (TextUtils.isEmpty(remake))
                        {
                            UiUtils.showToast("备注不能为空");
                            return;
                        }
                        break;
                    case MODE_OVERPASS:
                        if (choosePosition.size()==0)
                        {
                            UiUtils.showToast("还没有选择桩啊");
                            return;
                        }
                        break;
                    case MODE_ALLUNPASS:
                        if (TextUtils.isEmpty(remake))
                        {
                            UiUtils.showToast("驳回全部,请填写备注");
                            return;
                        }
                        break;
                }
                if (TextUtils.isEmpty(remake))
                {
                    remake = "null";
                }
                if (onCommitInspectionListener!=null)
                    onCommitInspectionListener.onCommitInspectionListener(mode,choosePosition,remake,counts,mErrorText.getText().toString());
                dismiss();
            }
        });

        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dismiss();
            }
        });
    }

    private OnCommitInspectionListener onCommitInspectionListener;
    public void setOnCommitInspectionListener(OnCommitInspectionListener onCommitInspectionListener)
    {
        this.onCommitInspectionListener = onCommitInspectionListener;
    }

    public interface OnCommitInspectionListener
    {
        void onCommitInspectionListener(int actionMode,List<Integer> choosePosition,String remake,int validPileCount,String errorMessage);
    }

    private void exeQuery(final String planNum,final ArrayList<String> pileNames)
    {
        final MyExecuteCallBack executeCallBack = new MyExecuteCallBack() {
            @Override
            public void onError(int code, String message)
            {
                handler.obtainMessage(ERROR).sendToTarget();
            }
            @Override
            public void onSuccess(Object o)
            {
                Message message = new Message();
                message.what = SUCESS;
                message.obj = o;
                handler.sendMessage(message);
            }
        };

        BaseAskTask baseAskTask = new BaseAskTask(new Runnable() {
            @Override
            public void run()
            {
                ArrayList<CommitBean> commitBeanArrayList = DbCenter.mHistoryDbHelper.queryIsUpLoadInPlan(planNum,pileNames);
                executeCallBack.onSuccess(commitBeanArrayList);
            }
        });
        baseAskTask.execute();
    }


    private static final int ERROR = -1;
    private static final int SUCESS = 1;

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case SUCESS:
                    progressBar.setVisibility(View.GONE);
                    contentView.setVisibility(View.VISIBLE);
                    ArrayList<CommitBean> commitBeens = (ArrayList<CommitBean>) msg.obj;
                    pileAdapter = new PileAdapter(commitBeens,getContext(), mode,onPileErrorMessage);
                    mPiles.setAdapter(pileAdapter);
                    break;
                case ERROR:
                    progressBar.setVisibility(View.VISIBLE);
                    contentView.setVisibility(View.GONE);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public interface OnPileErrorMessage
    {
        void onPileErrorMessage(String message);
    }
    private OnPileErrorMessage onPileErrorMessage = new OnPileErrorMessage() {
        @Override
        public void onPileErrorMessage(String message)
        {
            mErrorText.setText(mErrorText.getText().toString() + message);
            mErrorText.setSelection(mErrorText.getText().toString().length());
        }
    };
}
